package com.ld.poetry.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ld.poetry.config.PoetryResult;
import com.ld.poetry.dao.TreeHoleMapper;
import com.ld.poetry.entity.TreeHole;
import com.ld.poetry.service.ProhibitedWordsService;
import com.ld.poetry.service.TreeHoleServer;
import com.ld.poetry.utils.PoetryUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: taotai
 * @PackageName: poetize
 * @Package: com.ld.poetry.service.impl
 * @Date: 2023/6/8 17:16
 * @need: 需求
 */
@Service
public class TreeHoleServerImpl extends ServiceImpl<TreeHoleMapper, TreeHole> implements TreeHoleServer {
    @Autowired
    TreeHoleMapper treeHoleMapper;


    @Autowired
    ProhibitedWordsService prohibitedWordsService;

    @Override
    public PoetryResult saveTreeHole(TreeHole treeHole,HttpServletRequest request) {
        String ipAddress = PoetryUtil.recordIp(request);
        if (StringUtils.hasText(ipAddress)) {
            treeHole.setIp(ipAddress);
//                && treeHoleMapper.selectCount(new LambdaQueryWrapper<TreeHole>()
//                .eq(TreeHole::getIp, ipAddress)
//                .eq(TreeHole::getMessage, treeHole.getMessage())) > 0) {
//            return PoetryResult.fail("请勿重复留言！");
        }

        if (!StringUtils.hasText(treeHole.getMessage())) {
            return PoetryResult.fail("留言不能为空！");
        }
        if (new PoetryUtil().checkProhibitedWords(treeHole.getMessage(),prohibitedWordsService.list())) {
            return PoetryResult.fail("留言中包含违禁词!!!");
        }

        treeHoleMapper.insert(treeHole);
        if (!StringUtils.hasText(treeHole.getAvatar())) {
            treeHole.setAvatar(PoetryUtil.getRandomAvatar(null));
        }
        return PoetryResult.success(treeHole);
    }
}
